home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WINGs / connect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-14  |  4.7 KB  |  210 lines

  1. /*
  2.  *  WINGs connect.c: example how to create a network client using WMConnection
  3.  * 
  4.  *  Copyright (c) 1999 Dan Pascu
  5.  * 
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25.  
  26. #include "WINGs.h"
  27.  
  28.  
  29.  
  30. static int initialized = 0;
  31.  
  32.  
  33.  
  34. static void didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr);
  35.  
  36. static void connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr);
  37.  
  38. static void didInitialize(ConnectionDelegate *self, WMConnection *cPtr);
  39.  
  40.  
  41.  
  42. static ConnectionDelegate socketDelegate = {
  43.     NULL,              /* data */
  44.     NULL,              /* didCatchException */
  45.     connectionDidDie,  /* didDie */
  46.     didInitialize,     /* didInitialize */
  47.     didReceiveInput,   /* didReceiveInput */
  48.     NULL               /* didTimeout */
  49. };
  50.  
  51.  
  52.  
  53. void
  54. wAbort(Bool foo) /*FOLD00*/
  55. {
  56.     exit(1);
  57. }
  58.  
  59.  
  60. static char*
  61. getMessage(WMConnection *cPtr)
  62. {
  63.     char *buffer;
  64.     WMData *aData;
  65.     int length;
  66.  
  67.     aData = WMGetConnectionAvailableData(cPtr);
  68.     if (!aData)
  69.         return NULL;
  70.     if ((length=WMGetDataLength(aData))==0) {
  71.         WMReleaseData(aData);
  72.         return NULL;
  73.     }
  74.  
  75.     buffer = (char*)wmalloc(length+1);
  76.     WMGetDataBytes(aData, buffer);
  77.     buffer[length]= '\0';
  78.     WMReleaseData(aData);
  79.  
  80.     return buffer;
  81. }
  82.  
  83.  
  84. static void
  85. inputHandler(int fd, int mask, void *clientData)
  86. {
  87.     WMConnection *cPtr = (WMConnection*)clientData;
  88.     WMData *aData;
  89.     char buf[4096];
  90.     int n;
  91.  
  92.     if (!initialized)
  93.         return;
  94.  
  95.     n = read(fd, buf, 4096);
  96.     if (n>0) {
  97.         aData = WMCreateDataWithBytes(buf, n);
  98.         WMSendConnectionData(cPtr, aData);
  99.         WMReleaseData(aData);
  100.     }
  101. }
  102.  
  103.  
  104. static void
  105. didReceiveInput(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
  106. {
  107.     char *buffer;
  108.  
  109.     buffer = getMessage(cPtr);
  110.     if (!buffer) {
  111.         fprintf(stderr, "Connection closed by peer.\n");
  112.         exit(0);
  113.     }
  114.  
  115.     printf("%s", buffer);
  116.  
  117.     wfree(buffer);
  118. }
  119.  
  120.  
  121. static void
  122. connectionDidDie(ConnectionDelegate *self, WMConnection *cPtr) /*FOLD00*/
  123. {
  124.     WMCloseConnection(cPtr);
  125.  
  126.     fprintf(stderr, "Connection closed by peer.\n");
  127.     exit(0);
  128. }
  129.  
  130.  
  131. static void
  132. didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
  133. {
  134.     int state = WMGetConnectionState(cPtr);
  135.     WMHost *host;
  136.  
  137.     if (state == WCConnected) {
  138.         host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
  139.         fprintf(stderr, "connected to '%s:%s'\n",
  140.                 host?WMGetHostName(host):WMGetConnectionAddress(cPtr),
  141.                 WMGetConnectionService(cPtr));
  142.         initialized = 1;
  143.         if (host)
  144.             WMReleaseHost(host);
  145.         return;
  146.     } else {
  147.         wsyserrorwithcode(WCErrorCode, "Unable to connect");
  148.         exit(1);
  149.     }
  150. }
  151.  
  152.  
  153. int
  154. main(int argc, char **argv) /*FOLD00*/
  155. {
  156.     char *ProgName, *host, *port;
  157.     int i;
  158.     WMConnection *sPtr;
  159.  
  160.     wsetabort(wAbort);
  161.  
  162.     WMInitializeApplication("connect", &argc, argv);
  163.  
  164.     ProgName = strrchr(argv[0],'/');
  165.     if (!ProgName)
  166.         ProgName = argv[0];
  167.     else
  168.         ProgName++;
  169.  
  170.     host = NULL;
  171.     port = "7000";
  172.  
  173.     if (argc>1) {
  174.         for (i=1; i<argc; i++) {
  175.             if (strcmp(argv[i], "--help")==0 || strcmp(argv[i], "-h")==0) {
  176.                 printf("usage: %s [host [port]]\n\n", ProgName);
  177.                 exit(0);
  178.             } else {
  179.                 if (!host)
  180.                     host = argv[i];
  181.                 else
  182.                     port = argv[i];
  183.             }
  184.         }
  185.     }
  186.  
  187.     printf("Trying to make connection to '%s:%s'\n",
  188.            host?host:"localhost", port);
  189.  
  190.     sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL);
  191.     if (!sPtr) {
  192.         wfatal("could not create connection. exiting");
  193.         exit(1);
  194.     }
  195.  
  196.     WMSetConnectionDelegate(sPtr, &socketDelegate);
  197.  
  198.     /* watch what user types and send it over the connection */
  199.     WMAddInputHandler(0, WIReadMask, inputHandler, sPtr);
  200.  
  201.     while (1) {
  202.         WHandleEvents();
  203.     }
  204.  
  205.     return 0;
  206.  
  207. }
  208.  
  209.  
  210.